package be.phl.obstacleapp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import be.phl.model.DBAdapterScreening;
import be.phl.animation.ActivitySwitcher;
import be.phl.layoutAdapter.ScreeningRow;
import be.phl.layoutAdapter.ScreeningRowAdapter;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.support.v4.app.NavUtils;

public class ScreeningSummaryActivity extends ListActivity implements
		ScreeningRowAdapter.Callbacks {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// get all screening from the database and add them to the list using an
		// adapter
		DBAdapterScreening db = new DBAdapterScreening(this);
		db.open();
		Cursor c = db.getAllScreenings();
		final List<ScreeningRow> screenings = new ArrayList<ScreeningRow>();
		while (c.moveToNext()) {
			try {
				screenings.add(new ScreeningRow(c.getInt(0), MainData
						.getDateformat().parse(c.getString(4)), c.getString(6)
						.equals("T"), c.getString(8), c.getString(9), c
						.getString(24) + " " + c.getInt(25)));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		db.close();

		ScreeningRowAdapter adapter = new ScreeningRowAdapter(this, screenings);
		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_screening_summary, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// @Override
	// protected void onResume() {
	// // animateIn this activity
	// ActivitySwitcher.animationIn(findViewById(R.id.totaal),
	// getWindowManager());
	// super.onResume();
	// }

	@Override
	public void onItemClicked(ScreeningRow selected) {
		// TODO Auto-generated method stub
		Intent screeningAct = new Intent(this, ScreeningActivity.class);

		screeningAct.putExtra("ScreeningId", selected.getScreeningId());

		this.startActivity(screeningAct);

	}

}
